package com.rxyb.hongyu.service.logic;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rxyb.core.constants.DbConstants;
import com.rxyb.hongyu.constants.HongYuConstants;
import com.rxyb.hongyu.db.dao.ThirdHongyuPullMapper;
import com.rxyb.hongyu.db.po.ThirdHongyuPull;
import com.rxyb.hongyu.gateway.dto.HongYuRsp;
import com.rxyb.hongyu.gateway.dto.HongyuBaseReq;
import com.rxyb.toolize.date.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author duyh
 * @date 2020/6/9
 */
@Service
public class ThirdHongyuPullLogic {

    /**
     * 鸿宇推送记录：非预约单的数据保存
     */
    @Autowired(required = false)
    private ThirdHongyuPullMapper thirdHongyuPullMapper;

    /**
     * 保存非预约服务中的检车进度、审核、及报告
     *
     * @param apiNo
     * @param unionKey
     * @param req
     * @param rsp
     * @param validate
     * @return 执行的行数
     */
    public Integer saveHyApiRecord(String apiNo, String unionKey, HongyuBaseReq req, HongYuRsp rsp, HongYuRsp validate) {
        ThirdHongyuPull record = new ThirdHongyuPull();
        record.setAddTime(DateTime.getDateTime());
        record.setUpdateTime(DateTime.getDateTime());
        record.setApiNo(apiNo);
        record.setUnionKey(unionKey);
        record.setMchntNo(req.getJyjgbh());
        record.setPlateType(req.getHpzl());
        record.setCheckTransNo(req.getJylsh());
        record.setVehiclePlateNo(req.getHphm());
        record.setRspState(validate.getRetcode());
        record.setReqMsg(JSONObject.toJSONString(req));
        record.setRspMsg(JSONObject.toJSONString(rsp));
        if (HongYuConstants.isSucc(validate.getRetcode())) {
            // 待处理
            record.setRowState(DbConstants.ROW_STATE_VALID);
        } else {
            // 无需处理
            record.setOptResult(validate.getRetmessage());
            record.setRowState(DbConstants.ROW_STATE_INVALID);
        }
        int cnt = thirdHongyuPullMapper.insert(record);
        if (cnt == 1) {
            return record.getId();
        }
        return -1;
    }

    /**
     * 修改执行结果
     *
     * @param id
     * @param rspCode
     * @param optResult
     * @return 执行的行数
     */
    public Integer updateHyApiRecord(Integer id, String rspCode, String optResult) {
        ThirdHongyuPull record = new ThirdHongyuPull();
        record.setId(id);
        record.setUpdateTime(DateTime.getDateTime());
        record.setRowState(DbConstants.ROW_STATE_INVALID);
        record.setOptResult(optResult);
        record.setRspState(rspCode);
        return thirdHongyuPullMapper.updateById(record);
    }

    /**
     * 查询该接口是否已推送过
     *
     * @param unionKey
     * @param apiNo
     * @return 已存在的行数
     */
    public int queryByUnionKey(String unionKey, String apiNo) {
        // 根据车牌号查询检车流水号
        QueryWrapper<ThirdHongyuPull> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ThirdHongyuPull::getUnionKey, unionKey);
        wrapper.lambda().eq(ThirdHongyuPull::getApiNo, apiNo);
        return thirdHongyuPullMapper.selectList(wrapper).size();
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public ThirdHongyuPull query(Integer id) {
        return thirdHongyuPullMapper.selectById(id);
    }

    /**
     * 查询某个检车流水对应的所有记录
     *
     * @param checkTransNo
     * @return
     */
    public List<ThirdHongyuPull> query(String checkTransNo) {
        // 查询某个检车流水对应的所有记录
        QueryWrapper<ThirdHongyuPull> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ThirdHongyuPull::getCheckTransNo, checkTransNo);
        wrapper.lambda().eq(ThirdHongyuPull::getRowState, DbConstants.ROW_STATE_VALID);
        return thirdHongyuPullMapper.selectList(wrapper);
    }

    /**
     * 移除记录
     *
     * @param id
     * @return
     */
    public int removeHyApiRecord(Integer id) {
        ThirdHongyuPull record = new ThirdHongyuPull();
        record.setId(id);
        record.setUpdateTime(DateTime.getDateTime());
        record.setRowState(DbConstants.ROW_STATE_INVALID);
        return thirdHongyuPullMapper.updateById(record);
    }

    /**
     * 移除记录
     *
     * @param endTime
     * @return
     */
    public int removeInvalid(Long endTime) {
        QueryWrapper<ThirdHongyuPull> wrapper = new QueryWrapper<>();
        wrapper.lambda().le(ThirdHongyuPull::getAddTime, endTime);
        return thirdHongyuPullMapper.delete(wrapper);
    }
}
